package com.donlim.dev.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.donlim.dev.annotation.PassToken;
import com.donlim.dev.dto.CommonResult;
import com.donlim.dev.dto.LoginParams;
import com.donlim.dev.dto.PageInfoDto;
import com.donlim.dev.dto.sql.SQLParams;
import com.donlim.dev.entity.SysUser;
import com.donlim.dev.enums.ResultCode;
import com.donlim.dev.exception.SysUserException;
import com.donlim.dev.service.RedisService;
import com.donlim.dev.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.HashMap;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 星
 * @since 2020-11-03
 */
@Controller
@RequestMapping("/sys-user")
public class SysUserController {

    @Value("${jwt.tokenHeader}")
    private String tokenHeader;

    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private RedisService redisService;

//    //测试查询
//    @RequestMapping(value = "/getInfoByUserCode")
//    @ResponseBody
//    public CommonResult getInfoByUserCode(String userCode){
//        QueryWrapper<SysUser> queryWrapper = new QueryWrapper<SysUser>().eq("UserCode",userCode);
//        SysUser sysUser = sysUserService.getOne(queryWrapper);
//        return CommonResult.success(sysUser);
//    }

    //测试分页查询
    @RequestMapping(value = "/list",method = RequestMethod.POST)
    @ResponseBody
    public CommonResult list(@RequestBody SQLParams sqlParams){
        PageInfoDto<SysUser> userPageInfoDto = sysUserService.list(sqlParams);
        return CommonResult.success(userPageInfoDto);
    }

    //测试新增/修改
    @RequestMapping(value = "/addOrEditUser",method = RequestMethod.POST)
    @ResponseBody
    public CommonResult addOrEditUser(@RequestBody @Valid SysUser sysUser){
        sysUser.setPassword("123456");
        boolean b = sysUserService.saveOrUpdate(sysUser);
        if (b) return CommonResult.success();
        return CommonResult.failed();
    }

    //测试redis缓存
    @RequestMapping(value = "redis_test",method = RequestMethod.POST)
    @ResponseBody
    public CommonResult redis_test(@RequestBody HashMap<String,String> map){
        String key = map.get("key");
        String value = map.get("value");
        redisService.set(key,value);
        Object o = redisService.get(key);
        return CommonResult.success(o);
    }

    /**
     * 登录后返回token
     * @param loginParams
     * @return
     */
    @RequestMapping(value = "/login",method = RequestMethod.POST)
    @ResponseBody
    @PassToken
    public CommonResult login(@RequestBody @Valid LoginParams loginParams) throws SysUserException {
        String token = sysUserService.login(loginParams.getUserCode(),loginParams.getPassword());
        if (token == null) return CommonResult.failed(ResultCode.VALIDATE_FAILED.getCode(),"用户名或密码错误");
        HashMap<String,String> map = new HashMap<>();
        map.put(tokenHeader,token);
        return CommonResult.success(map);
    }

    /**
     * 获取用户信息
     * @param request
     * @return
     */
    @RequestMapping(value = "/info",method = RequestMethod.GET)
    @ResponseBody
    public CommonResult getUserInfo(HttpServletRequest request) throws SysUserException {
        SysUser userInfo = sysUserService.getUserInfo(request);
        return CommonResult.success(userInfo);
    }

    /**
     * 登出
     * @return
     */
    @RequestMapping(value = "logout",method = RequestMethod.POST)
    @ResponseBody
    public CommonResult logout(){
        return CommonResult.success();
    }

}
